package cn.com.czpb.client.module.live.view;

import android.app.Activity;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;

import cn.com.czpb.client.R;
import cn.com.czpb.client.custorm.TopPicViewPager;
import cn.com.czpb.client.model.live.LiveItemBean;
import cn.com.czpb.client.module.live.adapter.TopviewpagerLiveAdapter;

import java.util.List;

import me.relex.circleindicator.CircleIndicator;

/**
 * Created by 猪猪侠 on 2017/7/25.
 */

public class TopViewPagerLIveView {
    TopPicViewPager testPager;//viewpager
    private CircleIndicator mGuideIndicator;
    private Handler viewHandler = new Handler();
    private MyRun switchTask;//切换线程
    private boolean isContinue = true;//是否可以自动切换
    private TopviewpagerLiveAdapter topAdapter;
    private int interval = 4;//播放间隔时间
    private View rootView;
    private Activity mActivity;

    public static TopViewPagerLIveView instance(Activity activity){

        return new TopViewPagerLIveView(activity);
    }

    public TopViewPagerLIveView(Activity activity){
        //
        this.mActivity = activity;
        initView(activity);
    }

    public View getView(){
        return rootView;
    }

    /**
     * @param context
     */
    private void initView(Activity context) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        rootView = layoutInflater.inflate(R.layout.header_live,null);
        testPager= rootView.findViewById(R.id.test_pager);
        mGuideIndicator = rootView.findViewById(R.id.indicator);
        topAdapter = new TopviewpagerLiveAdapter(context);
        testPager.setAdapter(topAdapter);
        viewHandler.removeCallbacksAndMessages(null);
        switchTask = new MyRun();
        testPager.setCurrentItem(0);
        testPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                if (position == topAdapter.getCount() - 1) {
//                    testPager.setCurrentItem(1, false);
                    testPager.setCurrentItem(topAdapter.getCount() - 1, false);
                    return;
                } else if (0 == position) {
//                    testPager.setCurrentItem(topAdapter.getCount() - 2, false);
                    testPager.setCurrentItem(0, false);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    public void reFreshFlashData(List<LiveItemBean> mViewPagelist){
        topAdapter.setData(mViewPagelist);
        mGuideIndicator.setViewPager(testPager);
        if (mViewPagelist.size() > 1) {
            testPager.setCurrentItem(0, false);
        }
        if (topAdapter.getListSize() > 1) {
            if (switchTask.isStart){
                viewHandler.removeCallbacks(switchTask);
            }
            switchTask.run();
        }
    }


    /**
     * 轮播定时滚动
     */
    /**
     * 轮播定时滚动
     */
    class MyRun implements Runnable {
        private boolean isStart = false;

        public void run() {
            try{
            if (isContinue && isStart) {
                int current = testPager.getCurrentItem();
                int total = testPager.getAdapter().getCount();
                if (total == current + 1) {
                    testPager.setCurrentItem(0, false);
                } else {
                    testPager.setCurrentItem(current + 1, true);
                }
            } else {
                isStart = true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }

            viewHandler.postDelayed(switchTask, interval * 1000);
        }
    }

}
